package com.rico.kok.service;

import com.rico.kok.entity.Label;
import com.rico.kok.entity.PostLabel;
import com.rico.kok.mapper.LabelMapper;
import com.rico.kok.mapper.PostLabelMapper;
import com.rico.kok.service.interf.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {

    @Autowired
    LabelMapper labelMapper;

    @Autowired
    PostLabelMapper postLabelMapper;

    //插入标签
    @Transactional
    public int insertLabel(String label) {
        Label cLabel = new Label();
        cLabel.setLabel(label);
        return labelMapper.insertLabel(cLabel);
    }

    //通过条件查找Label
    public List<Label> selectLabel(PostLabel postLabel) {
        List<PostLabel> postLabelList = postLabelMapper.selectPostLabel(postLabel);
        List<Integer> list = new ArrayList<>();
        for (PostLabel pl :
                postLabelList) {
            list.add(pl.getLabelId());
        }
        return labelMapper.selectLabel(list);
    }

    public List<Label> selectAllLabel(){
        return labelMapper.selectAllLabel();
    }
}
